//index.js
const app = getApp()
var xrsdk = require('../../vendor/hope-sdk/core/request');
var config = require('../../config');
const util = require('../../utils/util.js');
var xrSdk = require('../../vendor/hope-sdk/index.js');
Page({
  data: {
    current: 0,
    banner: [],
    banner2: [],
    hot: [],
    news: [],
    category: [],
    newProduct: [],
    productList: [],
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,

    first:true
  },
  onLoad: function (options) {
    var that = this;
    app.pageOnLoadInit(this).then(function (res) {
      //这里写验证登录成功后且无需验证授权 需要执行的逻辑
      //若还需验证授权成功才执行的逻辑需写在onShow方法里面，
      //并且这里pageOnLoadInit()第二个参数要为 true
      app.checkMessageCount();


      that.initBanner();
      that.initBanner2();
      that.initHot();
      that.initNew();
      that.initList();
    }, function (error) {
      // 登录失败
      wx.showModal({
        title: "Error",
        content: error.errMsg ? error.errMsg : 'Fail to login.Please feedback to manager.',
      });
      return false;
    });
  },
  initBanner: function () {
    var that = this;
    xrsdk.request(app,{
      url: config.service.advertUrl,
      login: false,
      data: {
        position: 51321
      },
      success: function (res) {
        that.setData({
          banner: res.data.data
        });
      }
    });
  },
  initBanner2: function () {
    var that = this;
    xrsdk.request(app,{
      url: config.service.advertUrl,
      login: false,
      data: {
        position: 51323
      },
      success: function (res) {
        that.setData({
          banner2: res.data.data
        });
      }
    });
  },
  initHot: function () {
    var that = this;
    xrsdk.request(app,{
      url: config.service.goodsList,
      login: false,
      data: {
        page: 1,
        page_size: 8,
        is_hot:1
      },
      success: function (res) {
        that.setData({
          hot: res.data.data.data
        });
      }
    });
  },
  initNew:function(){
    var that = this;
    xrsdk.request(app,{
      url: config.service.goodsList,
      login: false,
      data: {
        page: 1,
        page_size: 9,
        is_new: 1
      },
      success: function (res) {
        that.setData({
          news: res.data.data.data
        });
      }
    });
  },
  initData: function () {
    this.setData({
      productList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true
    })
  },
  initList: function () {
    var that = this;
    xrsdk.request(app,{
      url: config.service.goodsList,
      login: false,
      data: {
        page: that.data.pageIndex,
        page_size: 10,
        is_recommend:1
      },
      success: function (res) {
        if (res.data.has_more == false) {
          this.setData({
            pullUpOn: false
          })
        }
        that.setData({
          productList: that.data.productList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        wx.stopPullDownRefresh()
      }
    });
  },
  onPullDownRefresh: function () {
    this.initData();
    this.initList();
  },
  onReachBottom: function () {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  detail: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '../goods/info/info?id=' + e.currentTarget.dataset.id
    })
  },
  onShow: function () {
    wx.hideTabBar({
      success: function () {
        app.editTabbar();
      }
    });

    if(!this.data.first){
      app.checkMessageCount();
    }
    this.setData({
      first:false
    });
    
  },
  tabbarSwitch: function (e) {
    if (e.detail != undefined) {
      wx.switchTab({
        url: e.detail.pagePath
      })
    }
  },
  search: function () {
    wx.navigateTo({
      url: '../goods/search/search'
    })
  },
  brandHandle:function(e){
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({
      url: url
    })
  },
  jobHandle: function (e) {
    wx.switchTab({
      url: '../category/category',
    })
  }
})